#ifndef _TCPSERVERAPP_H_
#define _TCPSERVERAPP_H_

#include <vector>
#include <sys/time.h>
#include "CTCPServer.h"
#include "FormatConversion.h"
#include "DB_MySql.h"

using namespace std;

void ConnectRUN(StreamSocket v_socket) ; //连接

class CServerApp
{
public:
    CServerApp();
	~CServerApp();

	StreamSocket     m_socket;                   //socket
    void  m_setConnectState(bool v_bState);
    bool  m_bGetConnectState();
    bool  m_bFormatSend(string v_strSend);
	bool  m_bFormatRecv(string &v_strRecv);
    void  m_WaitOtherThreadDestory();
    pthread_mutex_t  m_MutexForDestory;          //互斥量
private:
	bool  m_bConnected;
	pthread_mutex_t  m_MutexForState;            //互斥量

};

class CServerAppForDevice :public CServerApp
{
public:
	 CServerAppForDevice(StreamSocket v_socket);
	~CServerAppForDevice();

    int     m_nDeviceId;
    bool    m_bSetCurtainState(int v_n,char v_chState);
    string  m_strGetCurtainState();
    bool    m_bSetSendFlag(bool v_bSendFlag);
    bool    m_bGetSendFlag();
    bool    m_bMovieCurtain();

    bool    m_bkeepAlive();
    int     m_nBit;
private:
    pthread_mutex_t  m_MutexForCurtainState;            //互斥量
    char m_pchCurtainState[16];

    pthread_mutex_t  m_MutexForSendFlag;                //互斥量
    bool m_bSendFlag;
};

class CServerAppForUser :public CServerApp
{
public:

	 CServerAppForUser();
	~CServerAppForUser();

	MySql_DB   *m_pMySql;

    CServerAppForDevice* m_pDevice;              //Device
    int   m_nLoginInfoId;
    int   m_nUserId;
    string m_strUserName;
	void  m_recvMessage();                       //处理请求
	void  m_ProcessRequest();                    //处理请求
	bool  m_bSortRequest(string v_str_recv);     //请求分类
	int   m_nGetRequestType(string v_str);

	bool  m_bProcessLogin(vector<string> v_mesaagePackage); //处理登录请求
	bool  m_bProcessConnectDevice(vector<string> v_mesaagePackage);
	bool  m_bProcessCurtainState(vector<string> v_mesaagePackage);
    bool  m_bProcessCurtainMove(vector<string> v_mesaagePackage);

    bool  m_bWriteLoginTime();
    bool  m_bWriteLogoutTime();

	bool  m_bPushRequestToList(string strRequest);
	bool  m_bGetOutRequestFormList(string &strRequest);
private:
    vector<string>   m_recvList;                 //待处理的请求
	pthread_mutex_t  m_MutexForRecvList;         //互斥量
};

class DeviceManager
{
public:
      DeviceManager();
      ~DeviceManager();

      MySql_DB   *m_pMySql;

      void AddDeviceNodeToList(CServerAppForDevice*v_DeviceNode);
      bool DeletDeviceNodeByDeviceId(int v_nDeviceId);
      CServerAppForDevice* GetpAppDeviceById(int v_nDeviceId);
private:

  vector<CServerAppForDevice*> DeviceNodeList;
  pthread_mutex_t  m_MutexForDeviceList;         //互斥量

};

bool  bFormatSend(StreamSocket v_socket,string v_strSend);
bool  bFormatRecv(StreamSocket v_socket,string &v_strRecv);
void  CommunicateWithDevice(StreamSocket v_socket);
void  CommunicateWithUser(StreamSocket v_socket);
short nAskRole(StreamSocket v_socket);
void* ThreadHandMessage(void* p);
string strGetLocalTime();

#endif
